package com.jstyle.jclifexd.daoManager;

import android.text.TextUtils;

import com.jstyle.jclifexd.dao.HealthDataDao;
import com.jstyle.jclifexd.model.HealthData;
import com.jstyle.jclifexd.utils.DbManager;
import com.jstyle.jclifexd.utils.SharedPreferenceUtils;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/9.
 */

public class HealthDataDaoManager {
    public static void insertData(HealthData healthData){
        DbManager.getInstance().getDaoSession().getHealthDataDao().insertOrReplace(healthData);
    }
    public static void insertData(List<HealthData>healthDataList){
        DbManager.getInstance().getDaoSession().getHealthDataDao().insertOrReplaceInTx(healthDataList);
    }

    public static List<HealthData> queryAllData(){

        QueryBuilder<HealthData> queryBuilder = DbManager.getInstance().getDaoSession().getHealthDataDao().queryBuilder();
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(address))return new ArrayList<>();
        return queryBuilder.where(HealthDataDao.Properties.Address.eq(address)).orderDesc(HealthDataDao.Properties.Time).list();
    }

    public static  List<HealthData> queryDayData(String startDate,String endDate){
        List<HealthData>healthDataList=new ArrayList<>();
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(startDate)||TextUtils.isEmpty(endDate)||TextUtils.isEmpty(address))return healthDataList;
        QueryBuilder<HealthData> queryBuilder = DbManager.getInstance().getDaoSession().getHealthDataDao().queryBuilder();
        healthDataList = queryBuilder.where( HealthDataDao.Properties.Address.eq(address),HealthDataDao.Properties.Time.between(startDate, endDate)).orderDesc(HealthDataDao.Properties.Time).list();
        return healthDataList;
    }
    public static  List<HealthData> queryDayDataAsc(String startDate,String endDate){
        List<HealthData>healthDataList=new ArrayList<>();
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(startDate)||TextUtils.isEmpty(endDate)||TextUtils.isEmpty(address))return healthDataList;
        QueryBuilder<HealthData> queryBuilder = DbManager.getInstance().getDaoSession().getHealthDataDao().queryBuilder();
        healthDataList = queryBuilder.where( HealthDataDao.Properties.Address.eq(address),HealthDataDao.Properties.Time.between(startDate, endDate)).orderAsc(HealthDataDao.Properties.Time).list();
        return healthDataList;
    }
    public static  List<HealthData> queryDayData(String queryDate){
        List<HealthData>healthDataList=new ArrayList<>();
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(queryDate)||TextUtils.isEmpty(address))return healthDataList;
        QueryBuilder<HealthData> queryBuilder = DbManager.getInstance().getDaoSession().getHealthDataDao().queryBuilder();
        healthDataList = queryBuilder.where(HealthDataDao.Properties.Address.eq(address), HealthDataDao.Properties.Time.between(queryDate+" 00:00:00" , queryDate+" 23:59:59")).orderAsc(HealthDataDao.Properties.Time).list();
        return healthDataList;
    }
    public static HealthData queryDataByDate(String queryDate){
        HealthData healthDataList=null;
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(queryDate)||TextUtils.isEmpty(address))return healthDataList;
        QueryBuilder<HealthData> queryBuilder = DbManager.getInstance().getDaoSession().getHealthDataDao().queryBuilder();
        return queryBuilder.where( HealthDataDao.Properties.Address.eq(address),HealthDataDao.Properties.Time.eq(queryDate)).unique();

    }
    public static  List<HealthData> queryData(long queryDate){
        List<HealthData>healthDataList=new ArrayList<>();
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(address))return healthDataList;
        QueryBuilder<HealthData> queryBuilder = DbManager.getInstance().getDaoSession().getHealthDataDao().queryBuilder();
        healthDataList = queryBuilder.where( HealthDataDao.Properties.Address.eq(address),HealthDataDao.Properties.FileName.eq(queryDate)).orderAsc(HealthDataDao.Properties.Time).list();
        return healthDataList;
    }
    public static  List<HealthData> queryYearData(String queryDate,String endDate){
        List<HealthData>healthDataList=new ArrayList<>();
        if(TextUtils.isEmpty(queryDate)||TextUtils.isEmpty(endDate))return healthDataList;
        QueryBuilder<HealthData> queryBuilder = DbManager.getInstance().getDaoSession().getHealthDataDao().queryBuilder();
        healthDataList = queryBuilder.where( HealthDataDao.Properties.Time.between(queryDate+" 00:00:00" , endDate+" 23:59:59")).orderAsc(HealthDataDao.Properties.Time).list();
        return healthDataList;
    }
    public static HealthData getLastHealthData() {
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(address))return null;
        QueryBuilder<HealthData> queryBuilder = DbManager.getInstance().getDaoSession().getHealthDataDao().queryBuilder();

        return queryBuilder.where(HealthDataDao.Properties.Address.eq(address)).list().size() == 0 ? null : queryBuilder.where(HealthDataDao.Properties.Address.eq(address)).orderDesc(HealthDataDao.Properties.Time).list().get(0);
    }
    public static void deleteAll(){
        DbManager.getInstance().getDaoSession().getHealthDataDao().deleteAll();;
    }
}
